<?php
	header ('Content-type: text/html; charset=UTF-8'); 
	include ('../config/validacao.php');
	include('../config/conexao.php');


	$ramal = '';
	$idsetor = 0;
	if(isset($_GET['id'])){
	
		

		// query the database table for zip codes that match 'term'
		$rs = mysql_query('SELECT idsetor, ramal, flag FROM ramais, setor WHERE setor_idsetor=idsetor AND idramais="'.$_GET['id'].'"');

		// loop through each zipcode returned and format the response for jQuery
		$dados = mysql_fetch_array($rs);
		$ramal = $dados['ramal'];
		$idsetor = $dados['idsetor'];
		$flag = $dados['flag'];
		
	}else{
		echo "<script>alert('Erro ao listar registro');</script>";
		echo "<script>window.location.href = 'e_ramal.php';</script>";
	}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"http://www.w3.org/TR/html4/strict.dtd">
<html lang="pt_BR">
<head>
	<meta charset="utf-8">
	<link rel="icon" type="image/png" href="../images/icone.png">
	<title><?php include('../titulo.php')?></title>
	<script type="text/javascript" src="../js/jquery-1.7.2.min.js"></script>
 	
	<link rel="stylesheet" href="../css/jquery-ui-1.8.21.custom.css" type="text/css" media="all" />
	<link rel="stylesheet" href="../css/index.css"  media="screen"> 
	<link rel="stylesheet" href="../bootstrap/css/bootstrap.min.css"  media="screen"> 
	
	<script type="text/javascript" src="../bootstrap/js/bootstrap.min.js"></script> 
	<script type="text/javascript" src="../js/bootstrap-inputmask.js"/></script>
	
    <script type="text/javascript" src="../js/jquery.validate.min.js"></script> 
	<script type="text/javascript" language="javascript" >
		$(document).ready(function(){
			$('#form').validate({  
				rules: {  
					ramal: { required: true}//, remote: 'valida_ramal.php' }
				},  
				messages: {  
					ramal: { required: 'Preencha o campo'}//, remote: ' Ramal jah existe!' }
				},  
				submitHandler: function( form ){  
					var dados = $( form ).serialize();  
					$.ajax({  
						type: "POST",  
						url: "update_ramal.php",  
						data: dados,  
						success: function( data )  
						{  
							alert( data );
							window.location.href = "e_ramal.php";
						}  
					});  
	  				return false;  
				}  
			}); 
		}); 
 $(function(){
	$("select#mascara").change(function(){
		var options = '<br>';
		if($(this).val()== 1){
				 options += '<input class="input-medium" type="text" name="ramal" value="<?php echo $ramal;?>"  data-mask="(99) 9999-9999"/>';
		}else{
				 options += '<input class="input-medium" type="text" name="ramal" value="<?php echo $ramal;?>" onkeypress="return SomenteNumero(event)"/>';
		}
		 $("#ramal").html(options);
	})

})
	</script>
	
</head>
<body>
	<div id="container">
		<div id="topo" style="text-align:left;position:absolute;">
			<?php include('../cabecalho.php') ?>
		</div>
		<br><br>
		<div id="body">
			<form id="form">
				<fieldset>
				<legend>Editar Ramal</legend>
				<label>Ramal</label>
				<input type="hidden" name="idramais" value="<?php echo $_GET['id'];?>">
				<select id="mascara" name="mascara" class="span3">
					<option value="1">Ramal</option>
					<option value="0">Telefone Diversos</option>
				</select>
				<span id="ramal"><br>
					<input class="input-medium" type="text" name="ramal" value="<?php echo $ramal;?>"  data-mask="(99) 9999-9999"/>
				</span>				
<!--<input class="input-medium" type="text" name="ramal" value="<?php echo $ramal;?>" data-mask="(43)9999-9999"/> -->
				
				<label>Setor</label>
				<select name="setor">
					<?php
						header ('Content-type: text/html; charset=UTF-8'); 
						include ('../config/conexao.php');
						 
						// query the database table for zip codes that match 'term'
						$rs = mysql_query('SELECT idsetor, sigla, nomeSetor FROM setor, secretaria
										WHERE  idsecretaria = secretaria_idsecretaria');

						// loop through each zipcode returned and format the response for jQuery
						$data = array();
						if ( $rs && mysql_num_rows($rs) )
						{
							while( $row = mysql_fetch_array($rs) )
							{
								if($idsetor == $row['idsetor']){
									echo '<option value="'.$row["idsetor"].'" SELECTED>'.utf8_encode($row["sigla"]).' - '.utf8_encode($row["nomeSetor"]).'</option>';
								}else{
									echo '<option value="'.$row["idsetor"].'">'.utf8_encode($row["sigla"]).' - '.utf8_encode($row["nomeSetor"]).'</option>';
								}
							}
						}
					?>
				</select>

                                <label>Mostrar/Ocultar</label>

				 <select name="ativo">
				<?php
				if($flag == 0){
                                        echo "<option value='0' SELECTED>Mostrar</option>";
                                        echo "<option value='1'>Ocultar</option>";
				}else{
                                        echo "<option value='0'>Mostrar</option>";
                                        echo "<option value='1' SELECTED>Ocultar</option>";
				}
				?>
                                </select>
	
				<br>
				<button type="submit" class="btn">Atualizar</button>
				</fieldset>
				</form>
<center>
<div style="width:900px;">
                                <table style="font-size: 10px;" cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered" id="example">
                                <thead>
                                        <th width="12%">Ramal</th>
                                        <th>Contato</th>
                                        <th>Cargo</th>
                                        <th>E-mail</th>
                                </thead>
                                <tbody>
                                <?php
					
					        $rs = mysql_query('SELECT `ramal` ,  `nomeContato` ,  `cargo` ,  `email`
                                                        FROM  `contato` ,  `ramais_has_contato` ,  `ramais`
                                                        WHERE idcontato = contato_idcontato AND ramais_idramais=idramais
                                                        AND idramais='.$_GET["id"]);


                                                if ( $rs && mysql_num_rows($rs) )
                                                {
                                                        while( $row = mysql_fetch_array($rs) )
                                                        {
                                                                echo "<tr class='odd gradeX'>";
                                                                echo "<td>".$row['ramal']."</td>";
                                                                echo "<td>".utf8_encode($row['nomeContato'])."</td>";
                                                                echo "<td>".utf8_encode($row['cargo'])."</td>";
                                                                echo "<td>".utf8_encode($row['email'])."</td>";
                                                                echo "</tr>";
                                                        }

                                                }
                                        
                                ?>
                                </tbody>
                                </table>
</div>
</center>

		</div>
	</div>
</body>
</html>
